<?php
class IndexController extends  Controller{


	public function index(){
		$this->getSmarty();
		$this->getTemplatePosts();

		require_once SERVICE . DS . 'ad' . DS . 'AdService.class.php';
		$service = new AdService ( $this->getDB () );
		$curCategory_name = $service->getImgshowHtml();
        $this->smarty->assign ( 'curCategory_name', $curCategory_name );
        require_once COMMON . DS  . 'Constant.class.php';
        $this->smarty->assign ( 'tags',Constant::getHotTag() );
		$this->smarty->assign ( 'pagesets', Constant::indexHeader() );
		$this->smarty->assign ( 'leastblogs',  self::getBlogList());
		$this->smarty->display("index.tpl");
	}

	public function getBlogList() {
		$table = 'posts';
		require_once SERVICE.DS."CommonLogicService.class.php";
		$common = new CommonLogicService($this->getDB());
		$page = 1;
		$rows = 3;
		$offset = ($page - 1) * $rows;
		$blog_lists = $common->dataPage($table, $offset, $rows," ORDER BY post_date DESC ",null);
		foreach ($blog_lists as $k => $blog) {
			$p_content = strip_tags($blog->post_content);
			if(strlen($p_content) > 50){
				$p_content = substr($p_content,0,50);
				$p_content = $p_content.".....";
			}
			$month = self::Num_Chinese(str_replace ("0", "",date("m",strtotime($blog->post_date))));
			$day = date("d",strtotime($blog->post_date));
			$result_bloglist[] = array (
					"month"=>$month,
					"day"=>$day,
					"post_author" => $blog->post_author,
					"post_date" => $blog->post_date,
					"post_content" => $p_content,
					"post_file_location" => $blog->post_file_location,
					"tags" => $blog->tags,
					"post_title" => $blog->post_title,
					"post_url" => WEBSITE_URL."post".DS."index?id=".$blog->ID
	
			);
		}
		return $result_bloglist ;
	}
	
	
	public function Num_Chinese($Num){
	$Month_E = array (
			1 => "January",
			2 => "February",
			3 => "March",
			4 => "April",
			5 => "May",
			6 => "June",
			7 => "July",
			8 => "August",
			9 => "September",
			10 => "October",
			11 => "November",
			12 => "December" 
	);
	return $Month_E [$Num];
}
	
	/**
	 *
	 * 获取文章前10条数据
	 */


	public function getTemplatePosts(){
		require_once SERVICE . DS . 'IndexService.class.php';

		$indexService = new IndexService( $this->getDB () );
		
        $result = $indexService->loadPosts4ListCategory(0,30);
		
		$this->smarty->assign ( 'posts_html', $result );
	}

	/**
	 *
	 * 点击查看更多加载数据(文章)
	 */
	public function loadPosts(){
		$this->getSmarty();
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 0;
        $rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 30;
        $click_count = isset ( $_POST ['order'] ) ? $_POST ['order'] : "";
        require_once SERVICE . DS . 'IndexService.class.php';
        $indexService = new IndexService( $this->getDB () );

        $order = empty($click_count)?"order by  post_date desc ":'order by  '.$click_count.' desc';
        $result = $indexService->loadPosts4ListCategory($page, $rows, $order, "", "load");
        echo json_encode ( $result );
	}



	
	public function saveConnects(){
		require_once SERVICE . DS . 'IndexService.class.php';
	
		$indexService = new IndexService( $this->getDB () );
		$id = uniqid();
		$name = $_POST['name'];
		$email = $_POST['email'];
		$phone = $_POST['phone'];
		$website = $_POST['website'];
		$message = $_POST['message'];
		
		if(empty($email)){
			echo 0;
		}
		$sql = "INSERT INTO connects(id,name,email,phone,website,message,date) VALUES('$id','$name','$email','$phone','$website','$message',SYSDATE())";
		$result = $indexService->saveConnects($sql);
		
		require_once COMMON . DS . 'SendMailUtil.class.php';
		$adress = "feiya.gao@yivadigital.com";
		$title = "Yiva has a new contact !!";
		$content = "Name:".$name."<br>Email:".$email."<br>Phone:".$phone."<br>WebSite:".$website."<br>Message:".$message;
		$sendstatus = SendMailUtil::sendmail ( $title, $content, $adress );
		echo json_encode ( $result );
	
	}
	
}